<template>
  <div class="login-index" style="margin: 30px;">
      <p>
          <span>用户名:</span> <input type="text" v-model="account">
      </p>
      <p class="mt-10">
          <span>密&nbsp;&nbsp;&nbsp;&nbsp;码:</span> <input type="text" v-model="password">
      </p>
      <p class="mt-20">
          <button @click="login">立即登录</button>
      </p>
  </div>
</template>

<script>
import $axios from '@/api/request';
import {adminRoutes,superRoutes} from '@/router/index';
export default {
    data() {
        return {
            account: 'super',
            password: 'super',
        }
    },

    methods: {
        login() {
            let obj = {
                account: this.account,
                password: this.password,
            }
            $axios.post('/account/login',obj).then(res=> {
                // 保存用户信息和token
                let {token} = res;
                let {type,username} = res.data;
                this.$store.commit("set_token",token);
                this.$store.commit("set_type",type);
                this.$store.commit("set_username",username); 

                // 根据用户类型添加路由
                if(type == 1) {
                    this.$router.addRoutes(superRoutes);
                    this.$store.commit('set_routes',superRoutes);
                } else {
                    this.$router.addRoutes(adminRoutes);
                    this.$store.commit('set_routes',adminRoutes);
                } 
                this.$router.push('/home/index');
            })
        }
    }
}
</script>

<style>

</style>